//
//  LHTopicsViewController.m
//  EPA
//
//  Created by 卢浩 on 16/4/18.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHTopicsViewController.h"
#import "LHNetWorkRequestManager.h"
#import "LHBaseModel.h"
#import "LHBaseTableViewCell.h"
#import "LHTopicsTableViewCell.h"
#import "LORefresh.h"
#import "LHTopicsDetailController.h"

@interface LHTopicsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *topicsData;

@end

@implementation LHTopicsViewController

- (void)dealloc
{
    [self.tableView removeObserver:[self.tableView defaultHeader] forKeyPath:@"contentOffset"];
    
    //底部
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"话  题";

    self.topicsData = [NSMutableArray array];
    
    [self loadDataWithStart:0];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"LHTopicsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"LHTopicsTableViewCell"];
    
    //添加下拉刷新
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        [weakSelf.topicsData removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadDataWithStart:0];
    }];
    
    //添加上提加载更多
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.topicsData.count;
        [weakSelf loadDataWithStart:count];
    }];    
}

- (void)loadDataWithStart:(NSInteger)start{
    NSDictionary *paramsDict = @{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"client":@"1",@"sort":@"addtime",@"limit":@(10),@"version":@"3.0.2",@"start":@(start),@"auth":[LHUserInfoManager shareManager].auth};
    [LHNetWorkRequestManager requestUrl:[NSURL URLWithString:TOPICLIST_URL] Methed:POST Params:paramsDict Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *topicsDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
//            NSLog(@"%@",topicsDict);
            NSDictionary *dataDict = topicsDict[@"data"];
            NSArray *listArray = dataDict[@"list"];
            NSMutableArray *temp = [NSMutableArray array];
            for (NSDictionary *topsicDict in listArray) {
                LHBaseModel *model = [LHBaseModel modelWithDict:topsicDict];
                [temp addObject:model];
            }
            //        NSLog(@"%@",temp);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.topicsData addObjectsFromArray:temp];
                [self.tableView reloadData];
                //刷新结束
                [[self.tableView defaultHeader] endRefreshing];
                //底部
                [[self.tableView defaultFooter] endRefreshing];
            });
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.topicsData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LHBaseTableViewCell *cell = [LHBaseTableViewCell cellWithTableView:tableView Model:self.topicsData[indexPath.row] IndexPath:indexPath];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LHTopicsDetailController *topicsDetailVC = [[LHTopicsDetailController alloc]init];
    LHTopicsModel *model = self.topicsData[indexPath.row];
    topicsDetailVC.contentid = model.contentid;
    [self.navigationController pushViewController:topicsDetailVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    LHTopicsModel *model = self.topicsData[indexPath.row];
    if (model.coverimg.length == 0 && model.content.length == 0) {
        return 140;
    }else{
        return 215;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
